package com.suxin.plugin.intergration.manager.loader;

import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Tang
 * @classname CompoundPluginLoader
 * @description [  ]
 * @date 2022/7/4 21:40
 */
public class CompoundPluginLoader implements PluginLoader{

    List<PluginLoader> loaders = new ArrayList<>();

    public CompoundPluginLoader add(PluginLoader loader) {
        loaders.add(loader);
        return this;
    }

    @Override
    public boolean isApplicable(Path pluginPath) {
        for (PluginLoader loader : loaders) {
            if (loader.isApplicable(pluginPath)) {
                return true;
            }
        }
        return false;
    }

    @Override
    public ClassLoader loadPlugin(Path pluginPath) {
        for (PluginLoader loader : loaders) {
            if (loader.isApplicable(pluginPath)) {
                return loader.loadPlugin(pluginPath);
            }
        }
        throw new RuntimeException("No PluginLoader for plugin '" + pluginPath);
    }
}